package com.dcivision.framework.taglib.channel;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;

public class AjaxDataFormat  {

  long formDate;
  long toDate;
  String format = null;
  Locale locale = null;
  
  public final static String FORMAT_AGO = "ago";
  public final static String FORMAT_REMAIN = "remain";
  public final static String FORMAT_DUE = "due";
  
  public final static String HOUR = "HH";
  public final static String MINUTE = "MM";
  public final static String SECOND = "SS";
  
  private final static long MILLIS_DAY = 24*60*60*1000;
  private final static long MILLIS_HOUR = 60*60*1000;
  private final static long MILLIS_MINUTE = 60*1000;
  private final static long MILLIS_SECODN = 1000;
  
  public AjaxDataFormat(Timestamp formDate,Timestamp toDate,String format){
    
    this.formDate = formDate.getTime();
    this.toDate = toDate.getTime();
    this.format = format;
    this.locale = Utility.getLocaleByString(SystemParameterFactory.getSystemParameter(SystemParameterConstant.LOCALE));
  }
  
 public AjaxDataFormat(long formDate,long toDate,String format){
   this.formDate = formDate;
   this.toDate = toDate;
   this.format = format;
   this.locale = Utility.getLocaleByString(SystemParameterFactory.getSystemParameter(SystemParameterConstant.LOCALE));
 }
 
  private String choiceFormat(){
   
    if(FORMAT_AGO.equals(format)){
      return MessageResourcesFactory.getMessage(locale, "workflow.label.ago");
    }
    if(FORMAT_REMAIN.equals(format)){
      return MessageResourcesFactory.getMessage(locale, "workflow.label.remain");
    }
    if(FORMAT_DUE.equals(format)){
      return MessageResourcesFactory.getMessage(locale, "workflow.label.overdue"); 
    }
    
    return "";
  }
  
 
 public String format(){
   
    String formatStr = choiceFormat();
    StringBuffer bstr = new StringBuffer();
    long time = toDate-formDate;
    if(time>MILLIS_DAY){
      bstr.append(getDateTimeFormat("dd-MMM").format(new Date(formDate))); 
    }else if(time<MILLIS_DAY&&time>MILLIS_HOUR){
      bstr.append(this.getDuration(formDate, toDate, HOUR)+" "+choiceTimeStr(HOUR)+" "+formatStr);
    }else if(time>0&&time<MILLIS_HOUR){
      bstr.append(this.getDuration(formDate, toDate, MINUTE)+" "+choiceTimeStr(MINUTE)+" "+formatStr);
    }
    return bstr.toString();
  }
  
 
  private int getDuration(long formTime,long toTime,String type){
    if(HOUR.equals(type))
      return (int) ((toTime-formTime)/MILLIS_HOUR);
    if(MINUTE.equals(type))
      return (int)((toTime-formTime)/MILLIS_MINUTE);
    if(SECOND.equals(type))
      return (int)((toTime-formTime)/MILLIS_SECODN);
    return 0;
  }
  
  private String choiceTimeStr(String type){
    if(HOUR.equals(type))
      return MessageResourcesFactory.getMessage(locale, "workflow.label.hours");
    if(MINUTE.equals(type))
      return MessageResourcesFactory.getMessage(locale, "workflow.label.minutes");
    if(SECOND.equals(type))
      return MessageResourcesFactory.getMessage(locale, "workflow.label.seconds");
    return "";
  } 
  
  private  SimpleDateFormat getDateTimeFormat(String patten) {
      
    String pattenStr = patten;
    if(Utility.isEmpty(pattenStr))
      pattenStr = SystemParameterFactory.getSystemParameter(SystemParameterConstant.DATETIME_FORMAT);
    if (!Utility.isEmpty(locale)) {
      return new SimpleDateFormat(pattenStr, locale);
    }        
    return new SimpleDateFormat(pattenStr);
  }
}
